package comu.hnevc.zh.testdemo.fragment;

import android.annotation.TargetApi;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import comu.hnevc.zh.testdemo.dialog.AccountDialog;
import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.bean.Car;
import comu.hnevc.zh.testdemo.MainActivity;
import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.adapter.AccountAdapter;

public class AccountManagerFragment extends Fragment {
    private String user;
    private ListView listView;
    private RequestQueue mQueue;

    private String[] caruser;
    private String[] carnumber;
    private int[] carband;
    public AccountAdapter adapter;
    List<Car> mCarlist;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.accountmanager_fragment,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //在fragment中获得了MainActivity的实例
        MainActivity activity= (MainActivity) getActivity();
        activity.allrecharge.setVisibility(View.VISIBLE);
        activity.allrecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              AccountDialog dialog=new AccountDialog(getActivity(),mCarlist);
              dialog.show();
            }
        });

        listView=getActivity().findViewById(R.id.lv_accountmanager);
        SharedPreferences spf=getActivity().getSharedPreferences("data",Context.MODE_PRIVATE);
        user=spf.getString("UserName","user2");
        mQueue=activity.requestQueue;
        initData();


    }

    private void initData() {
        mCarlist=new ArrayList<>();
        caruser=new String[]{"1","2","3","4"};
        carnumber=new String[]{"001","002","003","004"};
        carband=new int[]{R.drawable.baoma,R.drawable.mazida,R.drawable.bengci,R.drawable.zhonghua};
        mCarlist=new ArrayList<>();
        for (int i=0;i<4;i++){
            Car car=new Car();
            car.setCarid(i+1);
            car.setCaruser(caruser[i]);
            car.setCarnum(carnumber[i]);
            car.setCardrow(carband[i]);
            car.setNeedchaerge(false);
            mCarlist.add(car);
        }

        initView();
        initblance();

    }
        //查询小车余额接口
    private void initblance() {
        for (int i=0;i<mCarlist.size();i++){
            final JSONObject object= new JSONObject();
            try {
                object.put("CarId",i+1);
                object.put("UserName",user);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.GetCarAccountBalance, object, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Log.e("查询余额",jsonObject.toString());
                    try {
                        String result=jsonObject.getString("RESULT");
                        if ("F".equals(result)){
                            Toast.makeText(getActivity(),"查询失败",Toast.LENGTH_LONG).show();
                            return;
                        }

                        Car car=mCarlist.get(object.getInt("CarId")-1);
                        car.setCarblance(jsonObject.getInt("Balance"));
                        if (object.getInt("CarId")==4){
                            adapter.setDataCars(mCarlist);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            mQueue.add(jor);
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void initView() {
        adapter=new AccountAdapter(getContext());
        listView.setAdapter(adapter);
        adapter.setDataCars(mCarlist);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mQueue=null;
    }
}
